{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.5      , 0.8660254, 1.       , 0.8660254, 0.5      ])"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "# sin cos tan sinh、cosh、tanh\n",
    "arr0 = np.array([1/6, 2/6, 3/6, 4/6, 5/6])*np.pi    \n",
    "arr0_sin = np.sin(arr0)\n",
    "arr0_sin"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### around "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-13.16845075,   2.66015842, -23.46945193],\n",
       "       [ -2.92946181,  -0.9073396 ,  -4.57458929],\n",
       "       [ 13.4100821 ,  -5.12826031,  21.69577025]])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1 = np.random.normal(loc=0, scale=10, size=(3,3))\n",
    "arr1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-10.,   0., -20.],\n",
       "       [ -0.,  -0.,  -0.],\n",
       "       [ 10., -10.,  20.]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.around(arr1, -1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-14.,   2., -24.],\n",
       "       [ -3.,  -1.,  -5.],\n",
       "       [ 13.,  -6.,  21.]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 向下取整\n",
    "np.floor(arr1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-13.,   3., -23.],\n",
       "       [ -3.,  -1.,  -5.],\n",
       "       [ 13.,  -5.,  22.]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.rint(arr1)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 算数运算\n",
    "- abs  绝对值\n",
    "- sqrt 开方\n",
    "- square 平方\n",
    "- exp 指数\n",
    "- log 、 log10 、log2 、 log1p 对数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[13.16845075,  2.66015842, 23.46945193],\n",
       "       [ 2.92946181,  0.9073396 ,  4.57458929],\n",
       "       [13.4100821 ,  5.12826031, 21.69577025]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.abs(arr1)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 判断是否为空"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([False, False,  True, False])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr_isnan = np.array([1,2,np.nan, 4])\n",
    "np.isnan(arr_isnan)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 二元函数\n",
    "- 算术运算\n",
    "- 比较运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[11 22 33 44 55]\n",
      "[ -9 -18 -27 -36 -45]\n",
      "[ 10  40  90 160 250]\n",
      "[0.1 0.1 0.1 0.1 0.1]\n"
     ]
    }
   ],
   "source": [
    "arr_1 = np.array([1,2,3,4,5])\n",
    "arr_2 = np.array([10,20,30,40,50])\n",
    "# 加减乘除\n",
    "print(np.add(arr_1, arr_2))\n",
    "print(np.subtract(arr_1, arr_2))\n",
    "print(np.multiply(arr_1, arr_2))\n",
    "print(np.divide(arr_1, arr_2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([  1,   8,  27,  64, 125], dtype=int32)"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数组3次方\n",
    "np.power(arr_1, 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([          1,     1048576, -1010140999,           0,  1296002393])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数组次方\n",
    "np.power(arr_1, arr_2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 0, 1, 0, 1], dtype=int32)"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 取余\n",
    "np.mod(arr_1, 2)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 比较运算\n",
    "- maximum\n",
    "- fmax\n",
    "- minimum\n",
    "- fmin"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[10 20 27 40 50]\n",
      "[10 20 26 40 50]\n"
     ]
    }
   ],
   "source": [
    "arr0 = np.array([10,20,26,40,50])\n",
    "arr1 = np.array([10,20,27,40,50])\n",
    "print(np.maximum(arr0, arr1))\n",
    "print(np.minimum(arr0, arr1))\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- greater、greater_equal\t相当于运算符：>、≥\n",
    "- less、less_equal\t相当于运算符：＜、≤\n",
    "- equal、not_equal\t相当于运算符：==、！="
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ True,  True,  True,  True,  True])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.greater(arr0, 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([False, False,  True, False, False])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.not_equal(arr0, arr1)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 统计函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [ 4,  5,  6,  7],\n",
       "       [ 8,  9, 10, 11],\n",
       "       [12, 13, 14, 15]])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr_statistic = np.arange(16).reshape(4,4)\n",
    "arr_statistic"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数组求和结果为： 146\n",
      "数组求平均结果为： 29.2\n",
      "数组求中位数结果为： 26.0\n",
      "数组求方差结果为： 202.56\n",
      "数组求标准差结果为： 14.232357499725756\n",
      "数组求最小值为： 10\n",
      "数组求最大值为： 50\n"
     ]
    }
   ],
   "source": [
    "print(\"数组求和结果为：\", np.sum(arr_statistic))\n",
    "print(\"数组求平均结果为：\", np.mean(arr_statistic))\n",
    "print(\"数组求中位数结果为：\", np.median(arr_statistic))\n",
    "print(\"数组求方差结果为：\", np.var(arr_statistic))\n",
    "print(\"数组求标准差结果为：\", np.std(arr_statistic))\n",
    "print(\"数组求最小值为：\", np.min(arr_statistic))\n",
    "print(\"数组求最大值为：\", np.max(arr_statistic))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数组水平方向求和结果为： [ 6 22 38 54]\n",
      "数组水平方向求平均结果为： [ 1.5  5.5  9.5 13.5]\n",
      "数组水平方向求中位数结果为： [ 1.5  5.5  9.5 13.5]\n",
      "数组水平方向求方差结果为： [1.25 1.25 1.25 1.25]\n",
      "数组水平方向求标准差结果为： [1.11803399 1.11803399 1.11803399 1.11803399]\n",
      "数组水平方向求最小值为： [ 0  4  8 12]\n",
      "数组水平方向求最大值为： [ 3  7 11 15]\n"
     ]
    }
   ],
   "source": [
    "# 二维数组按水平轴 聚合计算\n",
    "print(\"数组水平方向求和结果为：\", np.sum(arr_statistic, axis=1))\n",
    "print(\"数组水平方向求平均结果为：\", np.mean(arr_statistic, axis=1))\n",
    "print(\"数组水平方向求中位数结果为：\", np.median(arr_statistic, axis=1))\n",
    "print(\"数组水平方向求方差结果为：\", np.var(arr_statistic, axis=1))\n",
    "print(\"数组水平方向求标准差结果为：\", np.std(arr_statistic, axis=1))\n",
    "print(\"数组水平方向求最小值为：\", np.min(arr_statistic, axis=1))\n",
    "print(\"数组水平方向求最大值为：\", np.max(arr_statistic, axis=1))\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 数组操作\n",
    "- flat\n",
    "- flatten\n",
    "- ravel"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "arr_handle = np.arange(8).reshape(2,4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10\n",
      "20\n",
      "26\n",
      "40\n",
      "50\n"
     ]
    }
   ],
   "source": [
    "# flat 为数组元素迭代器\n",
    "for element in arr0.flat:\n",
    "    print(element)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4, 5, 6, 7])"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr_handle.flatten(order='C')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.8 (tags/v3.9.8:bb3fdcf, Nov  5 2021, 20:48:33) [MSC v.1929 64 bit (AMD64)]"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "6fff98fc3b3d81bd655c2cc48858186e4d9e2db7b515bf1c3221888f12a62f87"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
